package com.blb.edutaskservice.config;

import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

@Component
public class AdUpdateJob {

    private static Logger logger = LoggerFactory.getLogger(AdUpdateJob.class);

    @XxlJob("updateAdJobHandler")
    public void updateAd() {
        try {
            //调用nginx中进行缓存更新的脚本
            URL url = new URL("http://192.168.223.166:8011/ad_update?sid=1");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("Content-Type", "application/json");

            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            logger.info("完成广告轮播图缓存更新{}",response.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
